<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body><pre>
<?php
include "db.conf.php";

//
$sExamDir = "exams/";
$sExamName="final";
if (($hExamDir = opendir($sExamDir))!==false)
{
	$aExamFiles =[];
	while ($sFile = readdir($hExamDir))
	{
		if (($sFile!==".") and ($sFile!==".."))
		{
			$sFileNumber = substr($sFile,0,strpos($sFile,"_"));
			$aExamFiles[$sFileNumber]=$sFile;			
		}
	}
	//print_r($aExamFiles);
}

//	Connnect to Database
$sTableName = "2014_w_la";
$sqlConn = new mysqli($sServer,$sUser,$sPassword,$sDatabaseName);
$sqlConn->query("SET NAMES 'UTF8'");

//  seclect students who attended exam
$sQuery = "select `student_number`,`icampus`,`team` from `".$sTableName."` where `exam_order`>0 order by `exam_order` asc";
$sqlResult = $sqlConn->query($sQuery);

$sKey=1;
while ($aRow = $sqlResult->fetch_array(MYSQLI_ASSOC))
{
	rename($sExamDir.$aExamFiles[$sKey++],$sExamDir.$aRow["student_number"]."_".$aRow["icampus"].".pdf");
}
?>
</pre>
</body>
</html>